-
Notifications
You must be signed in to change notification settings - Fork 46
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add the ability to mark the top level #61
base: master
Are you sure you want to change the base?
Conversation
I don't understand. Can you explain this in more detail? |
The goal of calling asap is to ensure the stack is cleared of all user code (i.e. next micro-tick). Any internal method that is asynchronous naturally clears the stack of all user code. e.g. console.log('a');
fs.readFile('foo.js', function () {
console.log('c');
asap(function () {
console.log('e');
});
console.log('d');
});
console.log('b'); as: console.log('a');
fs.readFile('foo.js', function () {
console.log('c');
console.log('d');
function () {
console.log('e');
}();
});
console.log('b'); This effectively removes the need for asap to call a more expensive nextTick implementation such as |
Sure. So why not just don't use asap in that case? I still don't understand the new API added. |
Because the two things are not necessarily related. i.e. my Promise library uses asap, but has no knowledge of fs.readFile. Someone could separately take the trouble to mark when things create fresh stack, and thereby make my promise library faster without my promise library needing to do the work itself of tracking all async operations. |
@ForbesLindesay Sorry if I'm missing something obvious, but can you please explain why this API would be better then an |
To be more clear, an |
You need to mark before |
Or I'm still missing something obvious, or my question was not clear enough. Consider asap.flushSync = function() {
flushing = true;
flush();
}; Why your first example is better then var fs = require('fs');
var asap = require('asap/raw');
var readFile = fs.readFile;
fs.readFile = function (filename, encoding, callback) {
if (callback === undefined) {
callback = encoding;
encoding = undefined;
}
readFile(filename, encoding, function (err, res) {
callback.apply(this, arguments);
asap.flushSync();
});
}; Can you please show a scenario where |
On a second tough, I'm not sure marking is even necessary if we flush synchronously, since it's only used to avoid recursive |
Ok, I see your point now. With @ForbesLindesay are you willing to add similar changes to |
Before we talk about API changes to support tail tasks, we need to addeess #56. At present, I would be embarrassed to promote anything based on ASAP, which for what it is worth, is everything I work on in my spare time these days. The debugging experience in Chrome is broken. Debugging is more important than speed, though we should strive to have both. That may entrain detecting whether the inspector is open (There is a package for that). I am interested in tail tasks, on the condition that we do not consume unbounded stack frames. I have not given this PR a lot of attention. Please continue discussing and summarizing. |
I gave a little more thought on this, and realized that the two function API is probably not adequate. We could define a asap.tailTask = function (task) {
flushing = true; // TODO: avoid flushing in asap if flushing == true
if (task) asap(task);
flush();
}; Now the first example becomes: var fs = require('fs');
var asap = require('asap/raw');
var readFile = fs.readFile;
fs.readFile = function (filename, encoding, callback) {
if (callback === undefined) {
callback = encoding;
encoding = undefined;
}
readFile(filename, encoding, function (err, res) {
asap.tailTask(callback.bind(this, err, res));
});
}; EDIT: asap.tailTask = fuction (task) {
rawAsap.markFlushing();
if (task) asap(task);
rawAsap.flush();
}; |
This is a request for comments. If people think it's worthwhile and agree with the proposed API, I'll add it to the other interfaces.
The idea is that you could do something like:
This avoids an extra nextTick call when we know we are already at the top of the stack.